package com.ruoyi.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;


/*
单个流程实例的挂起和激活
1.3流程实例的挂起和激活
对单个流程实例执行挂起操作，某个流程实例挂起则次流程不再继续执行，完成该流程实例的当前任务将报异常。
 类：SuspendProcessInstance2
 */
public class SuspendProcessInstance2 {
    public static void main(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //3.查询流程定义对象 yanruTODO这里的id是哪个表的id
        ProcessInstance holiday = runtimeService.createProcessInstanceQuery().processInstanceId("27505").singleResult();
        //4.得到当前流程定义的实例是否都为暂停状态
        boolean suspended = holiday.isSuspended();
        //5.判断
        if (suspended){//如果是暂停则激活
            runtimeService.activateProcessInstanceById(holiday.getId());
            System.out.println("流程实例"+holiday.getId()+"激活");
        } else {//如果是激活则暂停
            runtimeService.suspendProcessInstanceById(holiday.getId());
            System.out.println("流程实例"+holiday.getId()+"挂起");
        }
    }


    //挂起之后进行测试
    public static void testSuspend()
    {
        //1.获取ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取taskService
        TaskService taskService = processEngine.getTaskService();
        //3.根据任务流程的key和任务负责人查询任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("myProcess_1")
                .taskAssignee("zhangsan").singleResult();
        System.out.println(task.getId());
        //执行任务
        taskService.complete(task.getId());

    }
}

